import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/widgets.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/ui/new_health/new_health_record_list_page.dart';
import 'package:ls_work/ui/select_old_page.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/widgets/common_widget.dart';

import '../../a_elder/models/elder_entity.dart';
import '../../viewmodel/new_health/new_health_sign_view_model.dart';
import 'new_health_report_list_page.dart';

class NewHealthSignPage extends BaseStatefulMvvmWidget {
  final ElderInfo? info;

  NewHealthSignPage({this.info});

  @override
  State<StatefulWidget> createState() => _NewHealthSignState();
}

class _NewHealthSignState
    extends BaseMvvmState<NewHealthSignViewModel, NewHealthSignPage> {
  @override
  Widget buildLoadingContentView() {
    return PopScope(
        child: Column(
          children: [
            newContent().setWeight(1),
            Gaps.line,
            SizedBox(
              height: 10,
            ),
            Row(
              children: [
                SizedBox(
                  width: 16,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.backClick();
                  },
                  child: CommonText('取消', textColor: Colors.grey),
                  style: ButtonStyle(
                      minimumSize: WidgetStateProperty.all(Size.fromHeight(40)),
                      shape: WidgetStateProperty.all(
                          StadiumBorder(side: BorderSide(color: Colors.grey)))),
                ).setWeight(1),
                SizedBox(
                  width: 16,
                ),
                TextButton(
                  onPressed: () {
                    viewModel.submitHealth();
                  },
                  child: CommonText('提交', textColor: Colors.white),
                  style: ButtonStyle(
                      minimumSize: WidgetStateProperty.all(Size.fromHeight(40)),
                      shape: WidgetStateProperty.all(StadiumBorder()),
                      backgroundColor:
                          WidgetStateProperty.all(Colours.app_main)),
                ).setWeight(2),
                SizedBox(
                  width: 16,
                ),
              ],
            ),
            SizedBox(
              height: 20,
            )
          ],
        ),
        canPop: false,
        onPopInvoked: (didpop) {
          if (!didpop) {
            viewModel.backClick();
          }
          // return Future(() => false);
        });
  }

  Widget oldContent() {
    return SingleChildScrollView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          TitleValueWidget(
                  title: '长者姓名',
                  value: viewModel.elder?.name ?? "",
                  titleWeight: 3,
                  contentWeight: 3,
                  arrow: Icon(Icons.keyboard_arrow_right))
              .onTap(() {
            viewModel.chooseElder();
          }),
          Gaps.line,
          TitleValueWidget(
            title: '床位号',
            value: viewModel.elder?.bedNo ?? "",
            titleWeight: 3,
            contentWeight: 3,
          ),
          Gaps.line,
          ...viewModel.listItem
              .map((e) => viewModel.itemWidget(e))
              .toList()
              .insertWidget(Gaps.line),
        ],
      ),
    );
  }

  Widget newContent() {
    return SingleChildScrollView(
      padding: EdgeInsets.symmetric(horizontal: 16),
      child: Column(
        children: [
          SizedBox(
            height: 10,
          ),
          (viewModel.elder == null
                  ? Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        CommonText('请选择长者',
                            textSize: 20, textColor: Colors.grey),
                        SizedBox(
                          height: 12,
                        ),
                        CommonText('去选择 >', textColor: Colors.blue).onTap(() {
                          viewModel.chooseElder();
                        })
                      ],
                    )
                  : Row(
                      children: [
                        ImageLoad(
                          "${HttpApi.imageHead}${viewModel.elder!.headimageId}",
                          fit: BoxFit.cover,
                          width: 70,
                          height: 70,
                          placeholder: 'assets/none.png',
                          errorImage: 'assets/none.png',
                        ).toCircle(borderWidth: 0.5, borderColor: Colors.grey),
                        SizedBox(
                          width: 15,
                        ),
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            CommonText('${viewModel.elder!.name}'),
                            SizedBox(
                              height: 18,
                            ),
                            CommonText('床位号：${viewModel.elder!.bedNo}')
                          ],
                        ).setWeight(1),
                        CommonText('重新选择', textColor: Colors.blue).onTap(() {
                          viewModel.chooseElder();
                        })
                      ],
                    ))
              .addToContainer(
                  padding: EdgeInsets.symmetric(horizontal: 12),
                  width: double.infinity,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Colors.white, borderRadius: 8.borderRadius),
                  constraints: BoxConstraints(minHeight: 120)),

          SizedBox(
            height: 10,
          ),

          // TitleValueWidget(
          //         title: '长者姓名',
          //         value: viewModel.elder?.name ?? "",
          //         titleWeight: 3,
          //         contentWeight: 3,
          //         arrow: Icon(Icons.keyboard_arrow_right))
          //     .onTap(() {
          //   viewModel.chooseElder();
          // }),
          // Gaps.line,
          // TitleValueWidget(
          //   title: '床位号',
          //   value: viewModel.elder?.bedNo ?? "",
          //   titleWeight: 3,
          //   contentWeight: 3,
          // ),
          // Gaps.line,
          GridView.count(
            crossAxisCount: context.screenSize.width > context.screenSize.height
                ? (context.screenSize.width / 220).toInt()
                : 2,
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            childAspectRatio: 2,
            mainAxisSpacing: 10,
            crossAxisSpacing: 10,
            padding: EdgeInsets.zero,
            children: viewModel.listItem
                .map((e) => Row(
                      children: [
                        Icon(
                          e.icon,
                          size: 32,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Column(
                          mainAxisSize: MainAxisSize.min,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            CommonText(e.titleName).addToContainer(
                                padding: EdgeInsets.symmetric(vertical: 0)),
                            Divider(
                              height: 14,
                            ),
                            Row(
                              children: [
                                CommonText(e.value, hintText: '请输入'),
                              ],
                            )
                                .addToContainer(
                                    decoration: BoxDecoration(
                                        color: Colours.bg_color,
                                        borderRadius: 4.borderRadius),
                                    width: double.infinity,
                                    padding: EdgeInsets.symmetric(
                                        horizontal: 8, vertical: 3))
                                .onTap(() {
                              viewModel.onItemClick(e);
                            })
                          ],
                        ).setWeight(1)
                      ],
                    ).onTap(() {}).addToContainer(
                        decoration: BoxDecoration(
                            color: Colors.white, borderRadius: 8.borderRadius),
                        padding: EdgeInsets.symmetric(horizontal: 12)))
                .toList(),
          ),

          SizedBox(
            height: 10,
          ),

          // Column(
          //   mainAxisSize: MainAxisSize.min,
          //   crossAxisAlignment: CrossAxisAlignment.start,
          //   children: [
          //     CommonText('备注信息'),
          //     SizedBox(
          //       height: 15,
          //     ),
          //     NoBorderInput(
          //             minLines: 4,
          //             hintText: '请输入备注信息',
          //             maxLength: 1000,
          //             onChanged: (str) {
          //               viewModel.remark = str;
          //             },
          //             keyboardType: CommonInputType.multiline,
          //             inEditModel: viewModel.inEditMode)
          //         .addToContainer(
          //             decoration: BoxDecoration(
          //                 color: Colours.bg_color,
          //                 borderRadius: 8.borderRadius),
          //             padding:
          //                 EdgeInsets.symmetric(horizontal: 10, vertical: 10))
          //   ],
          // ).addToContainer(
          //     width: double.infinity,
          //     decoration: BoxDecoration(
          //         color: Colors.white, borderRadius: 8.borderRadius),
          //     padding: EdgeInsets.symmetric(horizontal: 15, vertical: 15))
        ],
      ),
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('生命体征登记'),
        actions: [
          TextButton(
              onPressed: () {
                // Go().push(NewHealthRecordListPage());
                Go().push(NewHealthReportListPage());
              },
              child: Text('体征记录'))
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  NewHealthSignViewModel createViewModel() {
    return NewHealthSignViewModel(context, info: widget.info);
  }

  @override
  void onRetryClick() {}
}
